#!/bin/sh
# PCP QA Test No. 1134
# Exercise pmrep/pcp2json/pcp2xml output with challenging values.
#
# Copyright (c) 2017 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pmrep >/dev/null 2>&1 || _notrun "pmrep not installed"
which pcp2json >/dev/null 2>&1 || _notrun "pcp2json not installed"
which pcp2xml >/dev/null 2>&1 || _notrun "pcp2xml not installed"

if which locale >/dev/null 2>&1
then
    case "$PCP_PLATFORM"
    in
	openbsd)
		if locale -m 2>&1 | grep '^UTF-8$' >/dev/null
		then
		    # we have UTF-8 support
		    :
		else
		    _notrun "No UTF-8 support in LOCALE"
		fi
		;;

	*)
		if locale charmap 2>&1 | grep '^UTF-8$' >/dev/null
		then
		    # we have UTF-8 support
		    :
		else
		    _notrun "No UTF-8 support in LOCALE"
		fi
		;;
    esac
fi

if [ $PCP_PLATFORM = freebsd ]
then
    # workaround datetime.strftime() in Python that is busted when
    # the timezone|timeinfo is, er, Australia/Someplace, for at least
    # some parts of the year
    #
    version=`uname -rs | sed -e 's/FreeBSD //'`
    case "$version"
    in
	14.[0-3]-*)
	    _notrun "datetime.strftime() botch"
	    # NOTREACHED
	    ;;
    esac
fi

status=1       # failure is the default!
signal=$PCP_BINADM_DIR/pmsignal
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

A="archives/value-test"

_value_filter()
{
    sed \
       -e "s,$A,ARCHIVE_PATH,g" \
       -e "s,XXX.*XXX,XXX,g" \
    #end
}

_sort()
{
    LC_COLLATE=POSIX sort
}

# real QA test starts here
echo "--- 00"
pmrep -z -p -u -a $A -w 40 kernel.all.uptime
echo "--- 01"
pmrep -z -p -u -a $A -w 40 sample.float.one
echo "--- 02"
pmrep -z -p -u -a $A -w 40 --ignore-unknown no.such.metric sample.float.one
echo "--- 03"
cat > $tmp.config <<EOF
[test]
no.such.metric = ,,
sample.float.one = ,,
EOF
pmrep -z -p -u -a $A -w 40 -c $tmp.config -5 :test
echo "--- 04"
# diminutive case - fail cleanly if no known metrics at all
pmrep -z -p -u -a $A -w 40 --ignore-unknown no.such.metric
echo "--- 05"
# -s 10 can be removed when Python 2 support is dropped
pmrep -z -p -u -a $A -w 40 -s 10 kernel.uname.release
echo "--- 06"
# -s 10 can be removed when Python 2 support is dropped
pmrep -z -p -u -a $A -w 40 -s 10 -l \; kernel.uname.release
echo "--- 07"
pmrep -z -p -u -a $A kernel.uname.release -o csv -r | sed -e 's,XXX.*XXX,XXX,g'
echo "--- 08"
pmrep -z -p -u -a $A kernel.uname.release -o csv -r | wc -lc | sed -e 's/  */ /g'
echo "--- 09"
# For completeness sake, no -nan in archives/value-test
pmrep -z -p -u -a archives/all-sles9-sp1.meta -w 10 filesys.full
echo "--- 10"
pcp2json -z -a $A -H -I "" | pmjson | _value_filter
echo "--- 11"
pcp2xml -z -a $A -H -I "" | _value_filter | _sort
echo "--- 12"

# success, all done
status=0
exit
